package cn.enilu.flash.api.controller.task;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.entity.task.HelpTask;
import cn.enilu.flash.bean.entity.task.Proof;
import cn.enilu.flash.bean.entity.task.TaskStep;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.task.AdminHelpTaskVo;
import cn.enilu.flash.service.task.HelpTaskService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.service.task.ProofService;
import cn.enilu.flash.service.task.TaskStepService;
import cn.enilu.flash.utils.StringUtil;
import cn.enilu.flash.utils.factory.Page;


import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/help/task")
public class HelpTaskController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private HelpTaskService helpTaskService;

	@Autowired
	private TaskStepService taskStepService;

	@Autowired
	private ProofService proofService;

	@GetMapping(value = "/list")
	@RequiresPermissions(value = "helpTask")
	public Ret list(@RequestParam(required = false) Long id,
					@RequestParam(required = false)String title,
					@RequestParam(required = false)Integer state,
					@RequestParam(required = false) Integer classifyId) {
		Page<HelpTask> page = new PageFactory<HelpTask>().defaultPage();
		page.addFilter("id",id);
		page.addFilter("classifyId",classifyId);
		page.addFilter("state",state);
		page.addFilter("title", SearchFilter.Operator.LIKE,title);
		page = helpTaskService.queryPage(page);
		List<HelpTask> records = page.getRecords();
		List<AdminHelpTaskVo> list = new ArrayList<>();
		for (HelpTask helpTask:records){
			AdminHelpTaskVo adminHelpTaskVo = new AdminHelpTaskVo();
			BeanUtils.copyProperties(helpTask,adminHelpTaskVo);
			List<TaskStep> taskStepList = taskStepService.queryAll(new SearchFilter("taskId", SearchFilter.Operator.EQ, helpTask.getId()));
			adminHelpTaskVo.setTaskStepList(taskStepList);
			List<Proof> proofList = proofService.queryAll(new SearchFilter("taskId", SearchFilter.Operator.EQ, helpTask.getId()));
			adminHelpTaskVo.setProofList(proofList);
			list.add(adminHelpTaskVo);
		}
		return Rets.success(page);
	}
	@GetMapping("pass")
	public Ret pass(Long taskId){
		HelpTask helpTask = helpTaskService.get(taskId);
		helpTask.setState(1);
		HelpTask update = helpTaskService.update(helpTask);
		return update == null?Rets.failure("审核失败！"):Rets.success();
	}
	@PutMapping("onPass")
	public Ret onPass(Long taskId,String failureReason){
		if(StringUtil.isNullOrEmpty(failureReason)) return Rets.failure("原因不能为空！");
		HelpTask helpTask = helpTaskService.get(taskId);
		helpTask.setState(3);
		helpTask.setFailureReason(failureReason);
		HelpTask update = helpTaskService.update(helpTask);
		return update == null?Rets.failure("审核失败！"):Rets.success();
	}

	@PostMapping
	@BussinessLog(value = "新增帮助任务", key = "name")
	@RequiresPermissions(value = "helpTaskAdd")
	public Ret add(@ModelAttribute HelpTask helpTask){
		helpTaskService.insert(helpTask);
		return Rets.success();
	}
	@PutMapping
	@BussinessLog(value = "更新帮助任务", key = "name")
	@RequiresPermissions(value = "helpTaskUpdate")
	public Ret update(@ModelAttribute HelpTask helpTask){
		helpTaskService.update(helpTask);
		return Rets.success();
	}
	@DeleteMapping
	@BussinessLog(value = "删除帮助任务", key = "id")
	@RequiresPermissions(value = "helpTaskDelete")
	public Ret remove(Long id){
		if (id == null) {
			throw new ApplicationException(BizExceptionEnum.REQUEST_NULL);
		}
		helpTaskService.delete(id);
		return Rets.success();
	}
}